package ee.az.tetris.worker;

import ee.az.tetris.Dispatcher;
import ee.az.tetris.Glass;
import ee.az.tetris.event.BlocksFixedEvent;
import ee.az.tetris.event.Event;
import java.awt.Color;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class FixationWorker extends Worker {

	private final Dispatcher dispatcher;

	public FixationWorker(Glass glass, Dispatcher dispatcher) {
		super(glass);
		this.dispatcher = dispatcher;
	}

	@Override
	public void work(Event event) {
		for (int col = 0; col < getGlass().getColumnCount(); col++) {
			for (int row = getGlass().getRowCount()-1; row >= 0 ; row--) {
				Color value = getGlass().getValue(col, row);
				if (Glass.FLOATING.equals(value)) {
					getGlass().setValue(Glass.FIXED, col, row);
				}
			}
		}
		dispatcher.dispatch(new BlocksFixedEvent());
	}
}
